package org.wx.io.netty.tcp.client;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.concurrent.EventExecutorGroup;
import lombok.extern.slf4j.Slf4j;
import org.wx.io.netty.tcp.codec.MyTcpProtocol;

import java.nio.charset.StandardCharsets;

/**
 * @description:
 * @author: wangxun
 * @createDate: 2021/2/22
 * @version: 1.0
 */
@Slf4j
public class TcpClientHandler extends SimpleChannelInboundHandler<MyTcpProtocol> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MyTcpProtocol msg) throws Exception {

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("通道激活...");

        for (int i = 0; i < 100; i++) {
            MyTcpProtocol myTcpProtocol = new MyTcpProtocol();
            myTcpProtocol.setCommand((byte)1);
            String contentStr = "新年好啊"+i;
            byte[] bytes = contentStr.getBytes(StandardCharsets.UTF_8);

            myTcpProtocol.setPackageLength(bytes.length);
            myTcpProtocol.setContent(bytes);

            ctx.writeAndFlush(myTcpProtocol);
        }



    }
}
